﻿using System.Collections.Generic;

using Vestras.StarCraft2.Luna.Parser;
using Vestras.StarCraft2.Luna.Parser.Ast;

namespace Vestras.StarCraft2.Luna.Validation {
    public sealed class TypedefTypeValidator : ITypeValidator {
        public System.Type[] Type {
            get {
                return new[] { typeof(Typedef) };
            }
        }

        public bool Validate(object value) {
            if (value != null && value is Typedef) {
                ErrorSink.UpdateChangeCollection();
                Typedef typedef = value as Typedef;
                IList<Entity> types = new List<Entity>(typedef.OriginalType.GetReferencedTypes());
                if (types.Count == 0) {
                    ErrorSink.AddError(ErrorSink.Error.Create("Cannot find type '" + typedef.OriginalType.GetQualifiedId() + "'", typedef.OriginalType));
                    return false;
                }

                string ambiguityEntities;
                if (types.IsAmbiguous(out ambiguityEntities, true)) {
                    ErrorSink.AddError(ErrorSink.Error.Create("Ambiguity between types " + ambiguityEntities, typedef.OriginalType));
                    return false;
                }

                return true;
            }

            return false;
        }
    }
}
